package br.com.rise.leilao

class LeilaoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [leilaoInstanceList: Leilao.list(params), leilaoInstanceTotal: Leilao.count()]
    }

    def create = {
        def leilaoInstance = new Leilao()
        leilaoInstance.properties = params
        return [leilaoInstance: leilaoInstance]
    }
	
	def springSecurityService
    
	def save = {
		// Codigo alterado para cadastrar um produto no leilao
		def produtoInstance = new Produto(params)
		
		if (produtoInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'produto.label', default: 'Produto'), produtoInstance.id])}"
		}
		else {
			render(view: "create", model: [leilaoInstance: leilaoInstance])
		}
		// cadastro do leilao
        def leilaoInstance = new Leilao(params)
		leilaoInstance.produto = produtoInstance
		leilaoInstance.dataInicio = new Date()
		// TODO logica pra data final
		leilaoInstance.dataTermino = new Date() + 5
		
		leilaoInstance.leiloeiro = springSecurityService?.currentUser
		
		
        if (leilaoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'leilao.label', default: 'Leilao'), leilaoInstance.id])}"
			sendMail{
				/*
				def usuarios = new ArrayList()
				def leilaoInstance = new Leilao(params)
				usuarios = br.com.rise.leilao.Usuario.list(categorias = leilaoInstance.produto.categorias)
				usuarios.each(){
					to usuarios.email
					*/
					to "andmedsantana@gmail.com" //comita essa parte e descomita o resto
					subject "Novo Leilao do seu interesse"
					//body "O produto params.nome esta sendo leiloado ate o dia params.dataTermino!"
					body "O produto ${leilaoInstance.produto.nome} esta sendo leiloado ate o dia ${leilaoInstance.dataTermino}.\n APROVEITEM!"
				//}
				}
			redirect(action: "show", id: leilaoInstance.id)
        }
        else {
            render(view: "create", model: [leilaoInstance: leilaoInstance])
        }
    }

    def show = {
        def leilaoInstance = Leilao.get(params.id)
        if (!leilaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
            redirect(action: "list")
        }
        else {
            [leilaoInstance: leilaoInstance, produtoInstance:leilaoInstance.produto]
        }
    }

    def edit = {
        def leilaoInstance = Leilao.get(params.id)
        if (!leilaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [leilaoInstance: leilaoInstance]
        }
    }

    def update = {
        def leilaoInstance = Leilao.get(params.id)
        if (leilaoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (leilaoInstance.version > version) {
                    
                    leilaoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'leilao.label', default: 'Leilao')] as Object[], "Another user has updated this Leilao while you were editing")
                    render(view: "edit", model: [leilaoInstance: leilaoInstance])
                    return
                }
            }
            leilaoInstance.properties = params
            if (!leilaoInstance.hasErrors() && leilaoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'leilao.label', default: 'Leilao'), leilaoInstance.id])}"
                redirect(action: "show", id: leilaoInstance.id)
            }
            else {
                render(view: "edit", model: [leilaoInstance: leilaoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def leilaoInstance = Leilao.get(params.id)
        if (leilaoInstance) {
            try {
                leilaoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leilao.label', default: 'Leilao'), params.id])}"
            redirect(action: "list")
        }
    }
}
